//页面头
document.write("<script type='text/javascript' src='"+root+"js/common/headerUtil.js?"+version+"'></script>");

var rootPath = getRootPath();
var isPhone =isPhoneScreen();
var isSchoolAccount = false;

$(function() {
	var windowH = $(window).height() - $("#smallDiv").height();
	if(!isPhone){
		$("#content").prop("src","basicInfo.html").ready();
		$(".navRight").css("display","flex");
	}else{
		$(".navRight").css("display","none");
	}
	
	if(isPhone){
		$("#accordion").height(windowH);
		$("#accordion").css("-webkit-overflow-scrolling","touch");
		$("#accordion").css("overflow","auto");
		$(".accordion").css("position","relative");
	}
	$("#focusSearch").prop("value","");
	if(isPhone){
		$("html").css({"width":"100%","overflow":"hidden"})
		$("body").css({"width":"100%","overflow":"hidden"})
	}
	
	//常用应用的事件函数
	headerMenuShow();//头部 导航  外部引用代码
//	login_reg($('#headerImg'))//头部 导航  外部引用代码
	var html = '<div class="login_btn">登录</div>';
	$('#headerImg').append(html);
	// 5.登录按钮
	$(".login_btn").click(function() {
		var url = rootPath + "/normal/pages/guest/login.html";
		document.location.href = url;
	});
	//获取用户的头像
	$.ajax({
		url: rootPath + '/user/queryUserself',
	    type: 'GET',
	    contentType: 'application/json;charset=UTF-8',
	    success: function (data) {
	     	if(data.returnCode == '0000') {
	      		var user = data.returnObject;
	      		//神策数据 传入登录ID
	      		try {
	      			sensors.login(user.uid);
		      		sensors.quick('autoTrack');
				} catch (e) {
					console.error("sensors error ==>",e)
				}
	      		
	      		var html = '';
				html = '<img class="headerImg" id="operate" src="'+rootPath+'/normal/images/operate.png"  alt=""/>';
				$('#headerImg').append(html);
				$(".headerImg").click(function() {
					var url = rootPath + "/normal/pages/member/menu.html";
					console.log("url=", url)
					document.location.href = url;

				})
	      		/*$("#headerAva").attr("src",rootPath + "/file/viewimg?fileName=" + (user.headimgurl) + "&type=1");*/
	      		if(isPhone){
					$(".wechatImg").attr("src",rootPath + "/file/viewimg?fileName=" + (user.headimgurl) + "&type=1");
					var uname = user.uname;
					if( !uname && $.trim(uname) == ''){
						uname = '用户昵称';
					} 
					$("#uname").html(uname);
	      		}
				
				$(".login_btn").hide();
				$(".wechatImg").show();
				
				//处理梦想教师身份 弹窗提醒 完善信息 
				var roles = user.roles;
				var isTeacher = false;
				if(roles){
					roles.map(function(item,index){
						if(item.rleId && item.rleId=="20181524903016560101"){
							isTeacher = true;
						}
						//学校账号  去除'实名认证'和'身份认证'菜单栏
						if(item.rleId && item.rleId == "20181529574197910125") {
							isSchoolAccount = true;
							$("#certifyLi").remove();
							$("#lastMenu").remove();
							//PC端  显示'学校信息'菜单栏
							if(!isPhone) {								
								$("#schoolLi").show();
							}
						}
						//增加运动梦想课任务菜单
						if(item.rleId && item.rleId == "20201572578634560015") {
							$("<li data-url=\"sportClass.html\">运动梦想课</li>").appendTo($("#taskList"));
							//增加事件
							$("#taskList li").click(function(){
								var url = $(this).data("url");
								if("task.html" == url) {
									showLoading();
									//parent.$("#content").prop("src",taskUrl).ready(hideLoading());				
									document.location.href = taskUrl;
								} else if("sportClass.html" == url) { 
									showLoading();
									//parent.$("#content").prop("src",sportClassTaskUrl).ready(hideLoading());	
									document.location.href = sportClassTaskUrl;
								}
								$("ul li").removeClass("currentli");
								$(this).addClass("currentli");
								if(isPhone){
									$(".navRight").css("display","flex");
									$("#accordion").slideUp(300);
									$("#content").show();
								}
							})
						}
					});
					if(isTeacher && isTeacher == true){
						if(!user.phone || !user.email || !user.unionid){
							layer.confirm("请完善您的个人信息：手机号/邮箱/微信。获取更多活动资讯！", {
										title:"提示",
										
										btn: ["去完善"]
										},
								function(){
									parent.$("#content").attr("src","securitySetting.html").ready();
									layer.closeAll();
									$("ul li").removeClass("currentli");
									$(this).addClass("currentli");
									
									if(isPhone){
										$(".navRight").css("display","flex");
										$("#accordion").slideUp(300);
										$("#content").show();
									}
								})
						}
					}
				}
				
	       	}else{
	       		setTimeout(function(){
	       			if (window.performance.navigation.type == 1) {
	       				console.log("页面第二次加载，依然没获取到用户信息");
	       				document.location.href='/account/logout/cas?service=/account'
	       			}else{
	       				console.log("首次被加载");
	       				location.reload();
	       			}
	       		},1000);
	       		
	       		$(".wechatImg").hide();
	       		$(".login_btn").show();
	       	}
	    },
	    error: function (jqXHR, textStatus, errorThrown) {
	      	layer.alert("error");
	    },
	    complete:function(){
	    	//参数方式跳转指定页面
	    	goToContentUrl();
	    }
	});
	
	$(".headInfo").click(function() {
		var r = $(this).next(".rightImg");
	  $(this).parent(".menu").siblings().find(".headInfo").next(".rightImg").removeClass("upImg");
		 $(this).parent(".menu").siblings().find(".headInfo").next(".rightImg").siblings("ul").slideUp(300)
			if(!r.hasClass("upImg")) {
				r.addClass("upImg");
				r.siblings("ul").slideDown(300);
			} else {
				r.removeClass("upImg");
				r.siblings("ul").slideUp(300);				
			}
	});
	$("#headerAva").click(function() {
		if(!isPhone){
			return;
		}
		var op = $(this);
		if(!op.is(':visible')){
			return;
		}
		//location.reload();
		if(op.hasClass("chg-Hui")) {
			$("#accordion").slideDown(300);
			op.removeClass("chg-Hui");
			$("#content").hide();
		} else {
			$("#accordion").slideUp(300);
			op.addClass("chg-Hui");
			$("#content").show();
		}
	});
	$("ul li").click(function() {
		var url = $(this).data("url");
		var direct = $(this).data("direct");
		var open = $(this).data("open");
		
		if(open){
			window.open(url);
			return;
		}
		if(direct){
			document.location.href = url;
			return;
		}
		//获取当前参数 判断是否跟列表地址 相同
		var contentUrl = getUrlParam("contentUrl");
		
//		if(contentUrl ==undefined || contentUrl != url){
//			document.location.href = "/account/normal/pages/member/menu.html?contentUrl=" + url;
//			return;
//		}
		
		
		if("certify.html" == url) {
			//学校账号
			if(isSchoolAccount) {
				layer.alert("学校账号无须认证");
				return;
			}
			var param = "";
			if(contentUrl !=undefined && contentUrl == url){
				var callbackUrl = getUrlParam("callbackUrl");
				if(callbackUrl){
					callbackUrl =  encodeURIComponent(callbackUrl);
					param = "?callbackUrl=" + callbackUrl;
				} else {
					param = "";
				}
			}
			$.ajax({
				url : rootPath + "/realNameAuth/dr/1/uid",
				method : "GET",
				success : function(data) {
					if('0000' == data.returnCode
							&& data.returnObject != null) {
						parent.$("#content").attr("src","certifyShow.html"+param).ready();
					} else {
						parent.$("#content").attr("src",url + param).ready();
					}
				}
			});
		} else if("bankInfo.html" == url) {
			var param = "";
			if(contentUrl !=undefined && contentUrl == url){
				var callbackUrl = getUrlParam("callbackUrl");
				if(callbackUrl){
					callbackUrl =  encodeURIComponent(callbackUrl);
					param = "?callbackUrl=" + callbackUrl;
				} else {
					param = "";
				}
			}
			
			$.ajax({
				url : rootPath + "/userDetailFront/queryDetailInfo",
				method : "GET",
				success : function(data) {
					if('0000' == data.returnCode 
							&& data.returnObject != null 
							&& data.returnObject.accountName != null
							&& data.returnObject.accountName != '') {
						parent.$("#content").prop("src","bankInfoShow.html" + param).ready();
					} else {
						parent.$("#content").prop("src",url + param).ready();
					}
				}
			});
		} else if("addressLists.html" == url) {
			if(contentUrl !=undefined && contentUrl == url){
				var callbackUrl = getUrlParam("callbackUrl");
				if(callbackUrl){
					callbackUrl =  encodeURIComponent(callbackUrl);
					parent.$("#content").prop("src","address.html?callbackUrl="+callbackUrl).ready();
				} else {
					parent.$("#content").prop("src",url).ready();
				}
			}else{
				parent.$("#content").prop("src",url).ready();
			}
		} else if("idAuthentication.html" == url) {
			//学校账号
			if(isSchoolAccount) {
				layer.alert("学校账号无须认证");
				return;
			}
			//跳转指定页面时 添加后不去查认证结果
			
			var param = "";
			if(contentUrl !=undefined && contentUrl == url){
				var callbackUrl = getUrlParam("callbackUrl");//getUrlParam 带转义
				if(callbackUrl){
					callbackUrl =  encodeURIComponent(callbackUrl);
					param = "callbackUrl=" + callbackUrl;
				} else {
					param = "";
				}
				var creType = getUrlParam("creType");
				if (creType !=undefined && creType != ''){
					var s = param== undefined || param == ''?'': '&';
					if(creType == 16 || creType == '16'){
						var uid = getUrlParam("uid");
						if(uid == undefined || uid== ''){
							layer.alert("认证链接错误，请重试")
							return;
						}
						url = url + "?urlCreType=" + 16 + s + param + "&uid="+uid;
						parent.$("#content").prop("src",url).ready();
					} else {
						creType = encodeURIComponent(decodeURIComponent(creType));
						url = url + "?urlCreType=" + creType + s +param;
						parent.$("#content").prop("src",url).ready();
					}
				}else{
					var s = param== undefined || param == ''?'': '?';
					parent.$("#content").prop("src",url + s + param).ready();
				}  
				
			}else{
				var s = param== undefined || param == ''?'': '?';
				parent.$("#content").prop("src",url + s + param).ready();
			}
			
			
		} else if("task.html" == url) { 
			showLoading();
			//parent.$("#content").prop("src",taskUrl).ready(hideLoading());				
			document.location.href = taskUrl;
		} else {
			parent.$("#content").prop("src",url).ready();
		}
		$("ul li").removeClass("currentli");
		$(this).addClass("currentli");
		
		if(isPhone){
			$(".navRight").css("display","flex");
			$("#accordion").slideUp(300);
			$("#content").show();
		}
	});
	
	$(".reg_btn").click(userReg);
	//5.登录按钮
	$(".login_btn").click(goLogin);
	var clientW = document.body.clientWidth;
	//打开导航栏
	//导航链接
	$(".applycation .app").click(function() {
		var url = $(this).data("url");
		window.open(url);
	})
	//搜索聚焦
	$(".removeIcon").hide();

	
	//删除搜索数据
	$(".removeIcon").click(function(){
		$("#focusSearch").prop("value","");
		$(".serachIcon").show();
		$(".removeIcon").hide();
	});
	//监听搜索框

	/*$("#focusSearch").keydown(function(e){
		if(e.keyCode==13){
			$("#searchIcon").click();
		}
	});*/
	
	$("#searchIcon").click(function(){
		$(location).prop('href', encodeURI('/account/normal/pages/search/search.html'));
	});

});
function goToContentUrl(){
	var contentUrl = getUrlParam("contentUrl");
	//学校账号 不跳转链接
	if(isSchoolAccount == true && contentUrl
			&& (contentUrl == "certify.html" || contentUrl == "idAuthentication.html")
	){
		return;
	}
	
	//跳转contentUrl页面 
	if(contentUrl != null && contentUrl != '') {
		var u = decodeURIComponent(contentUrl);
		$("[data-url='"+u+"']").click();
	}
}

function focusSearch(e){
	if(e !== ''){
		$(".removeIcon").show();
	}else{
		$(".removeIcon").hide();
	}
	
}
// 获取iframe子页面内容高度给iframe动态设置高度
function iFrameHeight() {
	var ifm = document.getElementById("content");
	var fullWindowH = $(window).height();
	var windowH = fullWindowH - $("#smallDiv").height();
	if(isPhone){
		var src = $("#content").attr("src");
		if(src != undefined && src.indexOf("task")!= -1){
			ifm.style.height = 0;
			ifm.style.minHeight = fullWindowH + 'px';
			ifm.style.height = fullWindowH + 'px';
		}
		return;
	}
	
	var subWeb = document.frames ? document.frames["content"].document
			: ifm.contentDocument;
	if (ifm != null && subWeb != null) {
		ifm.style.height = 0;// 关键这一句，先取消掉之前iframe设置的高度
		ifm.style.minHeight = windowH + 'px';
		if (subWeb.body.scrollHeight == 0) {
			ifm.style.height = $(window).height() + 'px';
		} else if (windowH > subWeb.body.scrollHeight) {
			ifm.style.height = windowH + 'px';
		} else {
			ifm.style.height = subWeb.body.scrollHeight + 'px';
		}
		

	}
//	console.log("ifm高度=",ifm.style.height);
};
function userReg() {
	var url = "../guest/reg.html";
	document.location.href = url;
}
//去登录页面
function goLogin() {
	var url = "../guest/login.html";
	document.location.href = url;
}

function menuClick(){
	console.log("======?")
}